package com.lanou.myapplication.PhotoRecyclerFragment;

import android.app.Activity;


import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;


import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.myapplication.PhotoRecyclerAdapter.FirstphotoRecyclerAdapter;
import com.lanou.myapplication.ViewpageActivity;
import com.lanou.myapplication.R;
import com.lanou.myapplication.entity.Photoone;


import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lanou.myapplication.Photointerface.OnItemClickListener;


/**
 * Created by dllo on 15/10/24.
 */
public class FirstphotoFragment extends Fragment implements OnItemClickListener {
    private RequestQueue queue;
    private FirstphotoRecyclerAdapter firstphotoReyclerAdapter;
    private RecyclerView fragmentView;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.activity_photo_recycleview1, null);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        queue = Volley.newRequestQueue(getActivity());
        fragmentView = (RecyclerView) getActivity().findViewById(R.id.fragmentView1);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 1);
        fragmentView.setLayoutManager(gridLayoutManager);

        firstphotoReyclerAdapter = new FirstphotoRecyclerAdapter(getActivity());
        firstphotoReyclerAdapter.setOnItemClickListener(this);
        fragmentView.setAdapter(firstphotoReyclerAdapter);
    }

    private void initData() {
        {
            String path = "http://api.sina.cn/sinago/list.json?channel=hdpic_toutiao&adid=4ad30dabe134695c3b7c3a65977d7e72&wm=b207&from=6042095012&chwm=12050_0001&oldchwm=&imei=867064013906290&uid=802909da86d9f5fc&p=1";
            StringRequest request = new StringRequest(path,
                    new Response.Listener<String>() {

                        @Override
                        public void onResponse(String response) {
                            Gson gson = new Gson();
                            Log.i("lanou", "----" + response);
                            Photoone rList = gson.fromJson(response, Photoone.class);

                            if (rList != null) {
                                List<Photoone.DataEntity.ListEntity> list = rList.getData().getList();
//                                for (Photothree.DataEntity.ListEntity listEntity : list) {
//                                    Log.d("ThirdphotoFragment", listEntity.getPic());
//                                }

                                if (list != null && list.size() > 0) {
                                    firstphotoReyclerAdapter.addData(list);

                                }

                            }


                        }


                    },
                    new Response.ErrorListener() {

                        @Override
                        public void onErrorResponse(VolleyError error) {
                            error.printStackTrace();

                        }
                    }) {
                @Override
                public Map<String, String> getHeaders()
                        throws AuthFailureError {
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("Accept-Encoding", "");
                    return map;


                }

                protected Response<String> parseNetworkResponse(NetworkResponse response) {
                    try {
                        String jsonString = new String(response.data, "UTF-8");
                        return Response.success(jsonString,
                                HttpHeaderParser.parseCacheHeaders(response));
                    } catch (UnsupportedEncodingException e) {
                        return Response.error(new ParseError(e));
                    } catch (Exception je) {
                        return Response.error(new ParseError(je));
                    }
                }
            };

            queue.add(request);
        }


    }

    @Override
    public void OnClick(Photoone.DataEntity.ListEntity data) {
        Intent intent = new Intent(getActivity(),ViewpageActivity.class);
//        for (int i = 0; i < data.getPics().getList().size(); i++) {
//            intent.putExtra("pic", data.getPics().getList().get(i).getPic());
//        }
       intent.putExtra("title",data.getTitle());
        intent.putExtra("id",data.getId());
        startActivity(intent);
    }
}


